// 测试附魔功能
console.log('=== 附魔功能测试 ===\n');

// 模拟装备数据
const testEquip = {
    id: "test_001",
    strengthenLevel: 1,
    uuid: "test-uuid",
    emoji: "🗡️",
    icon: "sword",
    quality: "F",
    name: "测试装备",
    desc: "用于测试的装备",
    solt_type: "武器",
    sys: "生物本质",
    type: "血族",
    job: "血骑士",
    count: 1,
    source: "测试",
    creator: "test",
    attribute: [
        { key: "物理攻击", val: 10 },
        { key: "最大生命值", val: 50 }
    ],
    skill: [], // 初始没有技能
    createTime: Date.now(),
    updateTime: Date.now()
};

console.log('1. 测试装备数据:');
console.log(`装备名称: ${testEquip.name}`);
console.log(`装备类型: ${testEquip.sys} - ${testEquip.type} - ${testEquip.job}`);
console.log(`装备位置: ${testEquip.solt_type}`);
console.log(`当前技能数量: ${testEquip.skill.length}`);
console.log('');

// 模拟附魔成功率计算
function getEnchantSuccessRate(equip) {
    const existingSkillCount = equip.skill.length;
    if (existingSkillCount === 0) {
        return 1.0; // 没有技能时100%成功
    }
    return 1 / existingSkillCount;
}

// 模拟附魔执行（包含重复技能检查）
function tryEnchant(equip) {
    const beforeSkillCount = equip.skill.length;
    const successRate = getEnchantSuccessRate(equip);
    
    console.log(`附魔前技能数量: ${beforeSkillCount}`);
    console.log(`成功率: ${Math.round(successRate * 100)}%`);
    
    // 随机判定是否成功
    const random = Math.random();
    const success = random <= successRate;
    
    console.log(`随机数: ${random.toFixed(3)}`);
    console.log(`判定结果: ${success ? '成功' : '失败'}`);
    
    if (success) {
        // 模拟添加技能
        const newSkill = {
            skillId: `skill_${Date.now()}`,
            reName: `测试技能${equip.skill.length + 1}`,
            desc: "这是一个测试技能",
            level: 1,
            maxLevel: 5,
            cooldown: 30,
            useCount: 0
        };
        
        // 检查是否与现有技能重复（双重保险）
        const existingSkillIds = equip.skill.map(s => s.skillId);
        if (existingSkillIds.includes(newSkill.skillId)) {
            console.log(`❌ 检测到重复技能: ${newSkill.skillId}`);
            return {
                success: false,
                message: '附魔失败，随机出的技能与装备现有技能重复',
                beforeSkillCount,
                afterSkillCount: beforeSkillCount,
                successRate
            };
        }
        
        equip.skill.push(newSkill);
        equip.updateTime = Date.now();
        
        console.log(`✅ 新增技能: ${newSkill.reName}`);
        console.log(`技能数量: ${beforeSkillCount} -> ${equip.skill.length}`);
    }
    
    return {
        success,
        message: success ? '附魔成功！' : '附魔失败',
        beforeSkillCount,
        afterSkillCount: equip.skill.length,
        successRate
    };
}

// 测试1: 初始状态附魔
console.log('2. 测试1 - 初始状态附魔 (无技能):');
const result1 = tryEnchant(testEquip);
console.log(`结果: ${result1.message}`);
console.log('');

// 测试2: 有1个技能时附魔
console.log('3. 测试2 - 有1个技能时附魔:');
const result2 = tryEnchant(testEquip);
console.log(`结果: ${result2.message}`);
console.log('');

// 测试3: 有2个技能时附魔
console.log('4. 测试3 - 有2个技能时附魔:');
const result3 = tryEnchant(testEquip);
console.log(`结果: ${result3.message}`);
console.log('');

// 测试4: 有3个技能时附魔
console.log('5. 测试4 - 有3个技能时附魔:');
const result4 = tryEnchant(testEquip);
console.log(`结果: ${result4.message}`);
console.log('');

// 测试5: 有4个技能时附魔
console.log('6. 测试5 - 有4个技能时附魔:');
const result5 = tryEnchant(testEquip);
console.log(`结果: ${result5.message}`);
console.log('');

// 测试6: 重复技能检查
console.log('7. 测试6 - 重复技能检查:');
// 手动添加一个重复的技能ID来测试
const duplicateSkill = {
    skillId: testEquip.skill[0].skillId, // 使用第一个技能的ID
    reName: "重复技能",
    desc: "这是一个重复的技能",
    level: 1,
    maxLevel: 5,
    cooldown: 30,
    useCount: 0
};

// 检查重复
const existingSkillIds = testEquip.skill.map(s => s.skillId);
if (existingSkillIds.includes(duplicateSkill.skillId)) {
    console.log(`❌ 检测到重复技能: ${duplicateSkill.skillId}`);
    console.log('结果: 附魔失败，随机出的技能与装备现有技能重复');
} else {
    console.log(`✅ 技能不重复: ${duplicateSkill.skillId}`);
    console.log('结果: 可以正常添加');
}
console.log('');

// 最终状态
console.log('8. 最终装备状态:');
console.log(`装备名称: ${testEquip.name}`);
console.log(`技能数量: ${testEquip.skill.length}`);
console.log('技能列表:');
testEquip.skill.forEach((skill, idx) => {
    console.log(`  ${idx + 1}. ${skill.reName} (ID: ${skill.skillId})`);
});

console.log('\n=== 测试完成 ===');
console.log('附魔功能测试说明:');
console.log('- 成功率 = 1 / 当前技能数量');
console.log('- 无技能时: 100% 成功');
console.log('- 1个技能时: 100% 成功');
console.log('- 2个技能时: 50% 成功');
console.log('- 3个技能时: 33.3% 成功');
console.log('- 4个技能时: 25% 成功');
console.log('- 技能越多，成功率越低，符合设计需求');
console.log('- 新增: 重复技能检查，避免添加重复技能'); 